package com.houdoudou.supplier.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

public class CommonFunction {
  
	public static  Map<String,Object>  UniversalHttpPost(String  params[] ){
		Map<String,Object> map= new HashMap<String,Object>();
        HttpResponse httpResponse;
        HttpEntity httpEntity;
        HttpClient httpClient = null;
        for(int i=0;i<3;i++)
       	System.out.println("params-------["+i+"]="+params[i]);
        NameValuePair pair1 =new BasicNameValuePair("affair", params[1]);
        String temp = null;
		try {
			temp = new String(params[2].getBytes(), "utf-8");
		//	temp=URLEncoder.encode( params[2].toString() );
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println("------------"+temp);
        NameValuePair pair2 = new BasicNameValuePair("jsonData",params[2]);
       
     
        List<NameValuePair> pairList = new ArrayList<NameValuePair>();
        pairList.add(pair1);
        pairList.add(pair2);
   
        InputStream  inputStream=null;
        String result="";
        try
        {
            HttpEntity requestHttpEntity = new UrlEncodedFormEntity(
                    pairList,HTTP.UTF_8);                  
            HttpPost httpPost = new HttpPost( params[0]);
          //  httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
            //httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
           // httpPost.addHeader("Authorization", mToken);
          //  httpPost.addHeader("Content-Type", "application/json");    
   
           //  httpPost.addHeader("Content-Type", "text/html");    //这行很重要
            // httpPost.addHeader("charset", HTTP.UTF_8);         //这行很重要
            // 将请求体内容加入请求中
            httpPost.setEntity(requestHttpEntity);
            // 需要客户端对象来发送请求
            httpClient = new DefaultHttpClient();
            // 发送请求
             httpResponse = httpClient.execute(httpPost);
             // 处理返回结果   
             final int responseCode = httpResponse.getStatusLine().getStatusCode();  
             if (responseCode == HttpStatus.SC_OK){
            // 显示响应
        	httpEntity=httpResponse.getEntity();
				 inputStream= httpEntity.getContent();
				BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
				String line=null;
				
				while((line=bufferedReader.readLine())!=null){
					result=result+line;	
				}
				result=result.trim();
				System.out.println("服务器返回数据：-----"+result);
				
				map.put("PostState", true);
				map.put("PostResult", result);
             }
             else{ System.out.println("network error");
                    map.put("PostState", false);
                    
             }
        } catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("can't connect to company's service");
				  map.put("PostState", false);
			/*	本广播要发送给主界面，暂时不发送，
             Intent intent = new Intent();//创建Intent对象  
             intent.setAction(Constants.ACTION_TOMAIN);  
       
             intent.putExtra("data", "can't connect to company's service");  
             sendBroadcast(intent);//发送广播  
             */
			}finally{
				try {
					if(inputStream!=null) inputStream.close();
					httpClient.getConnectionManager().shutdown();  
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}   
             
             
            
		   return map;
	   }
	
	public static String spellOrderStateWord(int orderState){
		String word="";
		if(orderState==1){ word="待支付";	}
		if(orderState==3){ word="已取消";	}
		if(orderState==4){ word="已取消";	}
		if(orderState==5){ word="已据单";	}
		if(orderState==6){ word="已取消";	}
		if(orderState==10){ word="待接单";	}
		if(orderState==11){ word="待柜确认";	}
		if(orderState==12){ word="待发货";	}
		if(orderState==20){ word="已发货";}
		if(orderState==21){ word="已送到";}
		if(orderState==22){ word="已收货";}
		if(orderState==23){ word="已收回";}
		if(orderState==24){ word="已丢弃";}
		return word;
	}
	 public static String genRandomNum(int pwd_len){  
		  //35是因为数组是从0开始的，26个字母+10个数字  
		  final int  maxNum = 10;  
		  int i;  //生成的随机数  
		  int count = 0; //生成的密码的长度  
		  char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };  
		    
		  StringBuffer pwd = new StringBuffer("");  
		  Random r = new Random();  
		  while(count < pwd_len){  
		   //生成随机数，取绝对值，防止生成负数，  
		     
		   i = Math.abs(r.nextInt(maxNum));  //生成的数最大为36-1  
		     
		   if (i >= 0 && i < str.length) {  
		    pwd.append(str[i]);  
		    count ++;  
		   }  
		  }  
		    
		  return pwd.toString();  
		 } 
	
		public static String spellRefundStateWord(int refundState){
			String word="";
			if(refundState==1){ word="等待商家处理";	}
			if(refundState==2){ word="已退款";	} 
			if(refundState==3){ word="已退款";	}
			if(refundState==4){ word="商家拒绝";	}	
			if(refundState==5){ word="未退款";	}
			if(refundState==6){ word="退款仲裁中";	}
			if(refundState==7){ word="已退款";	}
			if(refundState==8){ word="未退款";	}
			if(refundState==9){ word="已退款";	}
			if(refundState==10){ word="退款取消";	}
			
		
			return word;
		}
		
		public static String spellRefundResultWord(int refundResult){
			String word="";
			if(refundResult==1){ word="处理中";	}
			if(refundResult==2){ word="退款成功";	} 
			if(refundResult==3){ word="退款失败";	}
			return word;
		}
		public static String spellRefundTypeWord(int type){
			String word="";
			if(type==1){ word="不想买了";	}
			if(type==2){ word="送货太迟";	} 
			if(type==3){ word="质量问题";	}
			if(type==4){ word="拒绝接单";	}
			if(type==5){ word="系统退款";	}
			return word;
		}
		
		public  static void   alert(Context context,String title,String message) {
		    Dialog dialog = new AlertDialog.Builder(context).setTitle(title)
		            .setMessage(message)
		            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
		                public void onClick(DialogInterface dialog, int which) {
		                   
		                }
		            }).create();
		    dialog.show();
		}
		 public static boolean isNumeric(String str){ 
			   for (int i = str.length();--i>=0;){ 
			   if (!Character.isDigit(str.charAt(i))){
			   return false; 
			   } 
			   }
			   return true; 
			   } 
		   
}
